﻿@using Maanfee.Dashboard.Domain.Entities.Communications;
@using Microsoft.AspNetCore.SignalR.Client;
@using Newtonsoft.Json
@inherits _BaseComponentView

<MudBadge Color="Color.Secondary" Dot="true" Overlap="true" Visible="@IsVisible">
	<MudMenu Icon="@Icons.Material.Outlined.Notifications" Color="Color.Inherit" AnchorOrigin="Origin.BottomLeft" TransformOrigin="Origin.TopCenter" PopoverClass="docs-layout-menu-shadow" ListClass="pa-2 docs-menu-list" LockScroll="true">
		<div class="d-flex justify-space-between align-center px-2">
			<MudText Align="Align.Right" Color="Color.Success" Typo="Typo.subtitle2">@DashboardResource.StringNotifications</MudText>
		</div>
		@if (Notifications != null && Notifications.Count() > 0)
		{
			foreach (var item in Notifications)
			{
				<MudMenuItem Class="px-2 py-0 rounded" OnClick="@(()=> SelectedChatClicked(item))">
					<MudText Typo="Typo.subtitle2">@item?.Title</MudText>
					<MudText Typo="Typo.body2">@($"{item.Message} • {item.Date}")</MudText>
					</MudMenuItem>
				<MudDivider Class="my-2" />
			}
		}
		else
		{
			<div class="d-flex justify-center align-center px-2 py-8 relative">
				<MudText Class="mud-text-secondary my-12">@DashboardResource.MessageNoMatchingRecordsFound :(</MudText>
			</div>
		}
	</MudMenu>
</MudBadge>

@code {

	private HubConnection HubConnection;

	private bool IsVisible = false;

	protected override async Task OnInitializedAsync()
	{
		var user = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User;
		var CurrentIdUser = user.Claims.Where(a => a.Type == "Id").Select(a => a.Value).FirstOrDefault();

		if (HubConnection == null)
		{
			HubConnection = new HubConnectionBuilder().WithUrl(Navigation.ToAbsoluteUri("/signalRHub")).Build();
		}

		if (HubConnection.State == HubConnectionState.Disconnected)
		{
			await HubConnection.StartAsync();
		}

		HubConnection.On<ChatMessage>("ReceiveChatNotification", (ChatMessage) =>
		{
			if (CurrentIdUser == ChatMessage.IdToUser)
			{
				IsVisible = true;

				//Snackbar.Add(string.Format(DashboardResource.SignalRReceiveChatNotification, ChatMessage.FromUser.Name), Severity.Info);
				Snackbar.Add(string.Format(DashboardResource.SignalRReceiveChatNotification, ChatMessage.FromUser.Name), Severity.Info, config =>
				{
					config.VisibleStateDuration = 10000;
					config.HideTransitionDuration = 500;
					config.ShowTransitionDuration = 500;
					config.Action = $"{DashboardResource.StringChat} ? ";
					config.ActionColor = Color.Info;
					config.Onclick = snackbar =>
					{
						IsVisible = false;
						Navigation.NavigateTo($"/chat/{ChatMessage.IdFromUser}");
						StateHasChanged();
						return Task.CompletedTask;
					};
				});

				Notifications.Add(new Notification
					{
						IdFrom = ChatMessage.IdFromUser,
						Title = string.Format(DashboardResource.SignalRReceiveChatNotification, ChatMessage.FromUser.Name),
						Message = ChatMessage.Message,
						Date = $"{ChatMessage.SendDate.ToDateString()} - {ChatMessage.SendDate.ToTimeString()}",
					});

				StateHasChanged();
			}
		});

		// *****************************************
	}

	private class Notification
	{
		public string IdFrom { get; set; }

		public string Title { get; set; }

		public string Message { get; set; }

		public string Date { get; set; }
	}

	private List<Notification> Notifications = new();

	private void SelectedChatClicked(Notification Notify)
	{
		Notifications.Remove(Notify);
		IsVisible = false;
		Navigation.NavigateTo($"/chat/{Notify.IdFrom}");
	}

}
